package cate.game.pvp.throne.data;

import cate.common.table.d.GDGradePvp;
import cate.common.table.d.GDThrone;
import cate.common.util.TimeTool;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

@NoteClass("王座争霸赛季基础数据")
@Slf4j
public class TBSeason {

	@NoteField("当前赛季")
	public int index;

	@NoteField("赛季开始时间")
	public long startTime;

	@NoteField("赛季结束时间")
	public long endTime;

	@NoteField("下次检测时间")
	public long checkTime;

	public void readFrom(TBSeason data) {
		this.index = data.index;
		this.startTime = data.startTime;
		this.endTime = data.endTime;
		this.checkTime = data.checkTime;
	}

	@JsonIgnore
	public boolean tick() {
		return checkTime != 0 && System.currentTimeMillis() >= checkTime;
	}


	public static TBSeason build() {
		int index = 0;
		long startTime = 0L;
		long endTime = 0L;
		long checkTime;
		long now = System.currentTimeMillis();
		//如果开启时间在未来
		if (GDGradePvp.START_TIME > now) {
			checkTime = GDGradePvp.START_TIME;
		} else {
			index = 1;
			startTime = GDGradePvp.START_TIME;
			endTime = startTime + GDThrone.WHOLE_SEASON_TIME;
			while (now >= endTime) {
				index++;
				startTime += GDThrone.WHOLE_SEASON_TIME;
				endTime += GDThrone.WHOLE_SEASON_TIME;
			}
			checkTime = endTime;
		}
		TBSeason base = new TBSeason();
		base.index = index;
		base.startTime = startTime;
		base.endTime = endTime;
		base.checkTime = checkTime;
		base.print();
		return base;
	}


	public void print() {
		StringBuilder sb = new StringBuilder();
		if (index > 0) {
			sb.append("[王座争霸]-当前赛季：").append(index);
			sb.append(" 开始时间：").append(TimeTool.getDF2().format(new Date(startTime)));
			sb.append(" 结束时间：").append(TimeTool.getDF2().format(new Date(endTime)));
			sb.append(" 检测时间：").append(TimeTool.getDF2().format(new Date(checkTime)));
		} else {
			sb.append("活动暂未开启");
			sb.append(" 检测时间：").append(TimeTool.getDF2().format(new Date(checkTime)));
		}
		log.info(sb.toString());
	}
}
